#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require


#include "../../../../core/shader_std.h"
#include "../../../../core/shader_built_in_functions.h"
#include "../../../../core/shader_color.h"

layout(std140, binding = DEF_BINDING_PROJECT) uniform U_Matrix0 {
	mat4 mat_ProjecView;
};

layout(std430, column_major, binding = DEF_BINDING_WIDGET_TextCoordID) readonly buffer U4_Buf {
	highp int m_Text_ID[];
};

// (UV偏移)  (顶边偏移 宽度)  (Zuv偏移 )
layout(std430, column_major, binding = DEF_BINDING_WIDGET_TextCoord) readonly buffer U5_Buf {
	highp ivec4 m_Text_Coord[];
};


 

layout(points) in;
layout(points, max_vertices=64) out;


in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 0) in vec4 vColor[];
layout(location = 1) in uint vOffset_CharUV[];
layout(location = 2) in uint vOffset_Char[];
layout(location = 3) in uint vCharNum[];
layout(location = 4) in uint vDrawType[];
layout(location = 5) in uint vAlignType[];
layout(location = 6) in float vIntensity[];


layout(location = 1) out VS_FS_VERTEX {
	vec4		gColor;
	flat ivec3	vUV_Offset;
	flat ivec2	vUV_Bound;
};

out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};



void main(void){
	// 位置  大小  文字ID  深度
	
	//mat3x2 fonts = m_Text_Coord[0];
	ivec4 fonts = m_Text_Coord[0];
	vec4 v = gl_in[0].gl_Position;
	
	uint strLen = vCharNum[0];
	if(strLen > 128) strLen = 128;

	//return;
	//间隔步进
	float advance = 0;
	for(int f=0; f<strLen; ++f){
		ivec4 fontsUV = m_Text_Coord[ vOffset_CharUV[0] + m_Text_ID[vOffset_Char[0] + f] ];
		//ivec4 fontsUV = m_Text_Coord[ 64 ];
		//ivec4 fontsUV = m_Text_Coord[ 1 ];
		
		vUV_Offset.xy = fontsUV.xy;
		vUV_Offset.z = (fontsUV.z<<16)>>16;
		vUV_Bound = ivec2(fontsUV.x, fonts.y);
		
		
		gl_Position = v;

		float width = (fontsUV.z>>16);

		switch(vAlignType[0]){
			case 1:{
				
				gl_Position.y += advance;
				gl_Position.x += (fonts.y - width) * 0.5;

				advance += fonts.y;
				break;
			}
			default:{
				gl_Position.x += advance;
				gl_Position.y += fontsUV.w & 0x0000ffff;

				advance += (fontsUV.z>>16) + 2;
				break;
			}
		}
		gl_Position.xy += fonts.y * 0.5;
		
		
		gl_Position = mat_ProjecView * gl_Position;
		gl_PointSize = gl_in[0].gl_PointSize;
		
		
		gColor = vColor[0];
		EmitVertex();
	}

	EndPrimitive();
	
	
}



 